<template>
  <zh-form ref="formRef" v-loading="state.loading" :model="state.formData" label-width="100px" :span="12" :rules="state.rules">
    <zh-form-input
      prop="id"
      label="配置id"
      type="text"
	  placeholder="请输入配置id"
      :disabled="props.disabled"
      v-model="state.formData.id"
    ></zh-form-input>

    <zh-form-input
      prop="userId"
      label="用户id"
      type="text"
	  placeholder="请输入用户id"
      :disabled="props.disabled"
      v-model="state.formData.userId"
    ></zh-form-input>

    <zh-form-input
      prop="datasourceId"
      label="默认数据源"
      type="text"
	  placeholder="请输入默认数据源"
      :disabled="props.disabled"
      v-model="state.formData.datasourceId"
    ></zh-form-input>

    <zh-form-input
      prop="connName"
      label="连接名"
      type="text"
	  placeholder="请输入连接名"
      :disabled="props.disabled"
      v-model="state.formData.connName"
    ></zh-form-input>

    <zh-form-input
      prop="prefixTable"
      label="去除表名前缀"
      type="text"
	  placeholder="请输入去除表名前缀"
      :disabled="props.disabled"
      v-model="state.formData.prefixTable"
    ></zh-form-input>

    <zh-form-select
	  label="生成功能"
	  :disabled="props.disabled"
	  placeholder="请选择生成功能"
	  prop="createFunc"
	  v-model="state.formData.createFunc"
	  :options="state.dictData.createFunc"
	></zh-form-select>

	<zh-form-checkbox
	  prop="createTypeList"
	  label="生成类型"
	  :disabled="props.disabled"
	  :options="state.dictData.createType"
	  v-model="state.formData.createTypeList"
	></zh-form-checkbox>

    <zh-form-input
      prop="packageName"
      label="项目包名"
      type="text"
	  placeholder="请输入项目包名"
      :disabled="props.disabled"
      v-model="state.formData.packageName"
    ></zh-form-input>

    <zh-form-input
      prop="baseclassId"
      label="基类ID"
      type="text"
	  placeholder="请输入基类ID"
      :disabled="props.disabled"
      v-model="state.formData.baseclassId"
    ></zh-form-input>

    <zh-form-input
      prop="baseClassCode"
      label="继承类名"
      type="text"
	  placeholder="请输入继承类名"
      :disabled="props.disabled"
      v-model="state.formData.baseClassCode"
    ></zh-form-input>

    <zh-form-select
	  label="生成方式"
	  :disabled="props.disabled"
	  placeholder="请选择生成方式"
	  prop="generatorType"
	  v-model="state.formData.generatorType"
	  :options="state.dictData.generatorType"
	></zh-form-select>

	<zh-form-radio
	  prop="permissionLevel"
	  label="权限级别"
	  :disabled="props.disabled"
	  :options="state.dictData.permissionLevel"
	  v-model="state.formData.permissionLevel"
	></zh-form-radio>

	<zh-form-radio
	  prop="formLayout"
	  label="表单布局"
	  :disabled="props.disabled"
	  :options="state.dictData.formLayout"
	  v-model="state.formData.formLayout"
	></zh-form-radio>

    <zh-form-input
      prop="backendPath"
      label="后端生成路径"
      type="text"
	  placeholder="请输入后端生成路径"
      :disabled="props.disabled"
      v-model="state.formData.backendPath"
    ></zh-form-input>

    <zh-form-input
      prop="popType"
      label="弹窗类型"
      type="text"
	  placeholder="请输入弹窗类型"
      :disabled="props.disabled"
      v-model="state.formData.popType"
    ></zh-form-input>

    <zh-form-input
      prop="frontendPath"
      label="前端生成路径"
      type="text"
	  placeholder="请输入前端生成路径"
      :disabled="props.disabled"
      v-model="state.formData.frontendPath"
    ></zh-form-input>

    <zh-form-input
      prop="templateMode"
      label="模版名称"
      type="text"
	  placeholder="请输入模版名称"
      :disabled="props.disabled"
      v-model="state.formData.templateMode"
    ></zh-form-input>

    <zh-form-input
      prop="author"
      label="作者"
      type="text"
	  placeholder="请输入作者"
      :disabled="props.disabled"
      v-model="state.formData.author"
    ></zh-form-input>

    <zh-form-input
      prop="version"
      label="项目版本号"
      type="text"
	  placeholder="请输入项目版本号"
      :disabled="props.disabled"
      v-model="state.formData.version"
    ></zh-form-input>

    <zh-form-input
      prop="email"
      label="邮箱"
      type="text"
	  placeholder="请输入邮箱"
      :disabled="props.disabled"
      v-model="state.formData.email"
    ></zh-form-input>

    <zh-form-input
      prop="options"
      label="其他配置信息"
      type="text"
	  placeholder="请输入其他配置信息"
      :disabled="props.disabled"
      v-model="state.formData.options"
    ></zh-form-input>

    <zh-form-input
      prop="treeParam"
      label="树配置参数"
      type="text"
	  placeholder="请输入树配置参数"
      :disabled="props.disabled"
      v-model="state.formData.treeParam"
    ></zh-form-input>

    <zh-form-input
      prop="reqBaseUrl"
      label="请求基础url"
      type="text"
	  placeholder="请输入请求基础url"
      :disabled="props.disabled"
      v-model="state.formData.reqBaseUrl"
    ></zh-form-input>

    </zh-form>
</template>

<script lang="ts" setup name="genUserConfigFormEdit">
import {  ref, onMounted, reactive, defineProps, defineEmits, defineExpose } from 'vue';
import { ElMessage } from 'element-plus';

//------------------定义子组件---------------------------
const formRef = ref();
//------------------接收传入参数(组件)-------------------
const props = defineProps({
  id: {
    type: String,
    default: '',
  },
  disabled: {
    type: Boolean,
    default: true,
  },
  btnLoding: Boolean,
});
const emits = defineEmits(['update:btnLoding', 'cancel', 'refreshTable']);
//------------------初始化------------------------------
onMounted(() => {
  getDict();
  getDetail();
});
//------------------内部变量定义-------------------------
const state = reactive({
  formData: {} as any,
  detailVisible: true,
  loading: false,
  rules: {
	connName: [{ required: true, message: '连接名不能为空', trigger: 'blur' }],
	prefixTable: [{ required: true, message: '去除表名前缀不能为空', trigger: 'blur' }],
	createFunc: [{ required: true, message: '生成功能不能为空', trigger: 'blur' }],
  },
  dictData: {} as any,
  areaCodeSelectTreeData: [] as any,
});

// 查询数据字典信息
const getDict = () => {
  const params = {} as any;
  params.dictFields = "createFunc,createType,generatorType,permissionLevel,formLayout";
  if(params.dictFields){
	  $api.common.getBatchDict.post(params).then((res) => {
		state.dictData = res.result;
	  });
  }
};

//获取详情
const getDetail = () => {
  if (!props.id) {
    return;
  }
  state.loading = true;
  $api.common.send
  .post('GenUserConfig0002', { id: props.id })
  .then((res: any) => {
    state.formData = res.result || {};
    state.loading = false;
  })
  .catch(() => {
    state.loading = false;
  });
};

//保存
async function save() {
  let checkFlag = await formRef.value.checkForm();
  if (checkFlag) {
    emits('update:btnLoding', true);
    if (state.formData.id) {
      $api.common.send
      .post('GenUserConfig0005', state.formData)
      .then(() => {
        ElMessage.success('修改成功');
        emits('update:btnLoding', false);
        emits('cancel', (state.detailVisible = false));
        emits('refreshTable');
      })
      .catch(() => {
        emits('update:btnLoding', false);
        emits('cancel', (state.detailVisible = true));
      });
    } else {
      $api.common.send
      .post('GenUserConfig0004', state.formData)
      .then(() => {
        ElMessage.success('新增成功');
        emits('update:btnLoding', false);
        emits('cancel', (state.detailVisible = false));
        emits('refreshTable');
      })
      .catch(() => {
        emits('update:btnLoding', false);
        emits('cancel', (state.detailVisible = true));
      });
    }
  }
}

//------------设置给父组件调用的数据/方法(组件)-----------
defineExpose({ save });
</script>
<style lang="scss" scoped></style>
